package com.chuangyang.fixboxmaster.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.Advert;
import com.chuangyang.fixboxmaster.ui.MessageCenterActivity;

import java.util.ArrayList;

public class ViewFlowAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<Advert> mAdverts;
    private LayoutInflater inflater;

    public ViewFlowAdapter(Context context, ArrayList<Advert> adverts) {
        super();
        this.mAdverts = adverts;
        this.mContext = context;
        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mAdverts.size();
    }

    @Override
    public Advert getItem(int position) {
        return mAdverts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.viewflowimage_item, null);
            holder.imageView = (ImageView) convertView.findViewById(R.id.viewflow_img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
         Advert advert=null;
        if(mAdverts.size()!=0){
            advert = getItem(position % mAdverts.size());
            Glide.with(AppData.getContext()).load(advert.pic).centerCrop().crossFade().
                    placeholder(R.drawable.ic_banner).into(holder.imageView);
            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
//                    filterDestination(advert);
                }
            });
        }

        return convertView;
    }

    class ViewHolder {
        public ImageView imageView;
    }


    public void filterDestination(Advert advert) {
        if (advert.type == 10001) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(advert.target));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppData.getContext().startActivity(intent);
        } else if (advert.type == 20001) {
            Toast.makeText(mContext, advert.desc, Toast.LENGTH_LONG).show();
        } else if (advert.type == 30000) {
//            Intent intent=new Intent();
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            AppData.getContext().startActivity(intent);
//        } else if (advert.type == 30001) {
//            Intent intent = new Intent();
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.putExtra(ModuleActivity.TRANSCATION_ID, HomeFragment.TRANSCATION_ID_REPAIR);
//            intent.putExtra(ModuleActivity.TYPE, ModuleActivity.REPAIR_ACTIVITY);
//            AppData.getContext().startActivity(intent);
//        } else if (advert.type == 30002) {
//            Intent intent = new Intent();
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.putExtra(ModuleActivity.TRANSCATION_ID, HomeFragment.TRANSCATION_ID_CLEAN);
//            intent.putExtra(ModuleActivity.TYPE, ModuleActivity.REPAIR_ACTIVITY);
//            AppData.getContext().startActivity(intent);
//        } else if (advert.type == 30003) {
//            //TODO 安装下单
//        } else if (advert.type == 30004) {
//            //TODO  移机下单
//        } else if (advert.type == 30005) {
//            Intent intent = new Intent();
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.setClass(AppData.getContext(), ModuleActivity.class);
//            intent.putExtra(ModuleActivity.TYPE, ModuleActivity.COUPON_LIST_ACTIVITY);
//            AppData.getContext().startActivity(intent);
//        } else if (advert.type == 30006) {
//            Intent intent = new Intent();
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.setClass(AppData.getContext(), ModuleActivity.class);
//            intent.putExtra(ModuleActivity.TYPE, ModuleActivity.FEEDBACK_ACTIVITY);
//            AppData.getContext().startActivity(intent);
        } else if (advert.type == 30007) {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setClass(AppData.getContext(), MessageCenterActivity.class);
            AppData.getContext().startActivity(intent);
        } else if (advert.type == 40001) {

        }
    }

//    广告图片分类（用户端）：
//    1.点击跳转到显示一个网页页面
//    默认是1是跳转到url页面,   4
//    type：1，target:http://www.example.com
//    2. 图片加弹出文本,点击后跳出提示弹框，显示消息
//    2是点击弹出提示消息    20001
//    3.点击跳转到某个应用下面，app的某个菜单下面
//    个人中心界面：30000
//    是维修下单界面，30001
//    是清洗下单界面  30002
//    是安装下单界面 30003
//    是移机下单界面30004
//    是查看优惠券，30005
//    是邀请好友      30006
//    是消息中心，30007
//    4.纯宣传图片，点击后没有反应
//    是纯图片显示    40001
}
